package com.woniuxy.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.user.dto.AddUserDto;
import com.woniuxy.user.dto.SystemUserDto;
import com.woniuxy.user.entity.SystemUser;
import com.woniuxy.user.service.ISystemUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 *  前端控制器
 */
@RestController
@RequestMapping
@Api(tags = "用户控制层")
public class SystemUserController {

    @Autowired
    private ISystemUserService systemUserService;

    @ApiOperation("手机号查询")
    @GetMapping("/getPhone")
    public SystemUserDto getUser(@RequestParam("userPhone") String userPhone) {
        SystemUserDto user = systemUserService.getUserByPhone(userPhone);
        return user;
    }

    @GetMapping("/getByUserSex")
    @ApiOperation("根据性别查询分页")
    public Page<SystemUser> getByUserSex(@RequestParam("userSex") Integer userSex, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        Page<SystemUser> page = systemUserService.getByUserSex(userSex, pageNum, pageSize);
        return page;
    }

    @GetMapping("/getByUserName")
    @ApiOperation("根据姓名查询分页")
    public Page<SystemUser> getByUserName(@RequestParam("userName") String userName, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        Page<SystemUser> page = systemUserService.getByUserName(userName, pageNum, pageSize);
        return page;
    }
    @PostMapping("/adduser")
    @ApiOperation("添加用户")
    public ResponseEntity addUser(@RequestBody AddUserDto addUserDto){
        systemUserService.addUser(addUserDto);
        return ResponseEntity.ok("添加成功");
    }

    @PostMapping("/batchDelete")
    @ApiOperation("批量删除")
    public ResponseEntity<String> batchDelete(@RequestBody List<Integer> ids) {
        int result = systemUserService.batchDelete(ids);
        if (result > 0) {
            return ResponseEntity.ok("批量删除成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("批量删除失败");
        }
    }
}
